package com.esunrising.druid;


import com.alibaba.druid.pool.DruidDataSource;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

public class DruidInitListener implements ServletContextListener {
    private DruidDataSource dataSource;

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        dataSource = new DruidDataSource();
        dataSource.setDriverClassName("dm.jdbc.driver.DmDriver");
        dataSource.setUrl("jdbc:dm://192.168.1.144:5236/FASTFAST?loginEncrypt=False&charsetEncoding=UTF-8");
        dataSource.setUsername("FASTFAST");
        dataSource.setPassword("Oraclebice502");

        dataSource.setInitialSize(5);
        dataSource.setMaxActive(20);
        dataSource.setMinIdle(5);
        dataSource.setMaxWait(60000);

        // 注册到 ServletContext，方便取用
        sce.getServletContext().setAttribute("dataSource", dataSource);
        System.out.println("[DruidInitListener] 数据源初始化完成！");
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        if (dataSource != null) {
            dataSource.close();
            System.out.println("[DruidInitListener] 数据源已关闭！");
        }
    }
}
